package com.zyx.crawlerdemo.jsoup.jsoupconnect;

import com.zyx.crawlerdemo.utils.BufferUtils;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

import java.io.BufferedInputStream;
import java.io.IOException;
/**
 * @author Yaxi.Zhang
 * @since 2021/8/11 15:57
 * desc: 图片下载案例
 */
public class JsoupConnectInputstream {
	
	public static void main(String[] args) throws IOException {
		String imageUrl = "https://www.makro.co.za/sys-master/images/h98/h64/9152530710558/06cf39e4-7e43-42d4-ab30-72c81ab0e941-qpn13_medium";
		Connection connect = Jsoup.connect(imageUrl);
		Response response = connect.method(Method.GET).ignoreContentType(true).execute();  
		System.out.println("文件类型为:" + response.contentType());
		// 如果响应成功，则执行下面的操作
		if (response.statusCode() ==200) {
			// 响应转化成输出流
			BufferedInputStream bufferedInputStream = response.bodyStream();
			// 保存图片
			BufferUtils.bufferToFile(bufferedInputStream, "image/1.jpg");
		}
	}


}
